$(document).ready(function() {
    status = getUrlParam("status");
    var douyin = isDouyinClient();
    if (!douyin) {
        errorNotice("请使用抖音访问", "当前环境异常，请使用抖音访问本链接!");
        return;
    } else {
        if (status == "create") {
            localStorage.setItem('mini_set', 1);
            var currentUrl = window.location.href;
            currentUrl = currentUrl.replace('?status=create', '');
            currentUrl = currentUrl.replace('&status=create', '');
            window.location.href = currentUrl;
            return;
        } else {
            var mini_settimes = localStorage.getItem('mini_set') || 2;
            if (mini_settimes == 1) {
                localStorage.setItem('mini_set', 3);
                var device = getDeviceType();
                if (device == "android") {
                    errorNotice("抖音卡片制作提醒", "抖音私信卡片首次必须使用<font color='#FD6666'>苹果手机</font>制作,制作成功后安卓和苹果手机均可分享转发使用。");
                    return;
                } else {
                    $("#cotainer").html('<img src="https://res.wailian1.cn/uploads/20230824/9089278.jpg" style="width:100%;"/>');
                    return;
                }
            } else {
                loadWebview();
            }
        }
    }
});

function loadWebview() {
    $("#cotainer").html('<iframe class="card-iframe" src="' + linkurl + '"/>');
}

function getDeviceType() {
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        return "iphone";
    } else if (/(Android)/i.test(navigator.userAgent)) {
        return "android";
    } else {
        return "pc";
    };
}

function isDouyinClient() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/aweme/i) == 'aweme') {
        return true;
    } else {
        return false;
    }
}

function errorNotice(title, message) {
    $("#cotainer").html('<div class="page"><div class="weui-msg"><div class="weui-msg__icon-area"><i class="weui-icon-warn weui-icon_msg-primary"></i></div><div class="weui-msg__text-area"><h2 class="weui-msg__title">' + title + '</h2><p class="weui-msg__desc">' + message + '</p></div></div></div>');
}

function getUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");

            if (arr != null && arr[0] == paraName) {
                return arr[1];
            }
        }
        return "";
    } else {
        return "";
    }
}